package groupon.com.bo.controller;

import groupon.com.util.BoNavigation;
import groupon.com.util.ViewConstants;
import groupon.common.util.CodeConstants;
import groupon.contents.bo.service.ContentTypeService;
import groupon.contents.model.ContentType;
import groupon.contents.model.Content;

import java.security.Principal;
import java.text.DateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.aspectj.apache.bcel.classfile.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/bo/content/")
public class ContentController extends BoNavigation{

	@SuppressWarnings("unused")
	@Autowired
	private ContentTypeService _contentTypeService;
	
	@ModelAttribute
	public ContentType newRequest(@RequestParam(required = false) Integer id){
		ContentType contentType = new ContentType();
		if(id != null){
			return _contentTypeService.findContentType(id);
		}
		return contentType;
		
	}
	
	@RequestMapping(value = "/content-type", method = RequestMethod.GET)
	public String viewContentType(ModelMap model){
		model.addAttribute("subTab", "definContent");
		List<ContentType> listContentType = _contentTypeService.getAll();
		model.addAttribute("contenttypes", listContentType);
		return ViewConstants.RETURN_CONTENTTYPE_PAGE;
	}
	
	@RequestMapping(value = "/create-content", method = RequestMethod.GET)
	public String viewCreateContent(ModelMap model){
		model.addAttribute("subTab", "createContent");
		List<ContentType> listContentType = _contentTypeService.getAll();
		model.addAttribute("contenttypes", listContentType);
		return ViewConstants.RETURN_CREATE_CONTENT_PAGE;
	}
	
	@RequestMapping(value = "/create-content", method = RequestMethod.POST)
	public @ResponseBody String submitAddContent(@RequestParam String name, @RequestParam String dataContent, Principal principal){
		Date date = new Date();
		Content content = new Content();
		content.setCreatedDate(date);
		content.setModifiedDate(date);
		content.setCreatedBy(principal.getName());
		content.setName(name);
		content.setData(dataContent);
		return CodeConstants.SUCCESS; 
	}
	
	@RequestMapping(value = "/add-contenttype", method = RequestMethod.POST)
	public String submitAddContentType(ContentType contentType, ModelMap model, Principal principal){
		Date date = new Date();
		contentType.setCreatedDate(date);
		contentType.setCreatedBy(principal.getName());
		contentType.setModifiedDate(date);
		_contentTypeService.save(contentType);
		return ViewConstants.REDIRECT_CONTENTTYPE_PAGE;
	}
	
	@RequestMapping(value = "/add-contenttype", method = RequestMethod.GET)
	public @ResponseBody String addContentType(@RequestParam String name, @RequestParam String dataContentType, Principal principal){
		Date date = new Date();
		ContentType contentType = _contentTypeService.findContentTypeByName(name);
		if(contentType != null){
			return CodeConstants.ERROR_EXIST;
		}
		contentType = new ContentType();
		contentType.setCreatedDate(date);
		contentType.setCreatedBy(principal.getName());
		contentType.setModifiedDate(date);
		contentType.setName(name);
		contentType.setData(dataContentType);
		_contentTypeService.save(contentType);
		return CodeConstants.SUCCESS;
	}
	
	@RequestMapping(value = "/edit-contenttype", method = RequestMethod.GET)
	public @ResponseBody ContentType viewEditContentType(ContentType contentType){
		return contentType;
	}
	
	@RequestMapping(value = "/get-contenttype", method = RequestMethod.GET)
	public @ResponseBody ContentType getContentType(ContentType contentType){
		return contentType;
	}
	
	@RequestMapping(value = "/edit-contenttype", method = RequestMethod.POST)
	public @ResponseBody String submitEditContentType(ContentType contentType, @RequestParam String name, @RequestParam String dataContentType, Principal principal){
		Date date = new Date();
		if(!contentType.getName().equals(name)){
			ContentType contentTypeDB = _contentTypeService.findContentTypeByName(name);
			if(contentTypeDB != null){
				return CodeConstants.ERROR_EXIST;
			}	
		}
		contentType.setCreatedBy(principal.getName());
		contentType.setModifiedDate(date);
		contentType.setName(name);
		contentType.setData(dataContentType);
		_contentTypeService.update(contentType);
		return CodeConstants.SUCCESS;
	}
	
	@RequestMapping(value =  "/delete-contenttype", method = RequestMethod.GET)
	public @ResponseBody String deleteContentType(ContentType contentType){
		System.out.println("id >>>>>>>" + contentType);
		if(_contentTypeService.delete(contentType.getId())){
			return CodeConstants.SUCCESS;
		}else{
			return CodeConstants.FAIL;
		}
	}
	
	@Override
	public String getSubTab() {
		return "";
	}

	@Override
	public String getTab() {
		// TODO Auto-generated method stub
		return "Content";
	}
	

}
